/**
 * add_province
 * 
 * 增加一个省
 * 
 */
function add_province() {
    var province = $('#provinces').val().trim()
    console.log(province)
    url = '/add_provinces.php'
    data = {
        'province': province
    }
    $.post(
        url,
        data,
        function(res) {
            if (res.Code == 0) {
                flush_province()
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}


/**
 * del_province
 * 
 * 删除一个省
 * 
 * 
 **/
function del_province(id) {

    layui.use(['layer'], function() {
        var layer = layui.layer

        layer.open({
            content: '是否要删除一个省（包括了生下面的市和区）？',
            btn: ['删除', '取消'],
            yes: function(index, layero) {
                url = '/del_provinces.php'
                data = {
                    'id': id
                }
                $.post(
                    url,
                    data,
                    function(res) {
                        console.log(res)
                        if (res.Code == 0) {
                            console.log(res)
                            window.location.href = "/"
                        } else {
                            layer.msg(res.Msg, {
                                icon: 5
                            });
                        }
                    }, 'JSON');
            },
            btn2: function(index, layero) {
                console.log(index)
                console.log(layero)
            }


        })


    })
}


/**
 * edit_province
 * 修改省的名称
 * 
 * */
function edit_province(id, name) {

    layui.use(['layer'], function() {
        var layer = layui.layer
        layer.prompt({
            title: '请输入要修改省的名字',
            value: name
        }, function(value, index, elem) {
            layer.close(index);
            console.log(value)
            url = '/edit_provinces.php'
            data = {
                'id': id,
                'provinces': value
            }
            $.post(
                url,
                data,
                function(res) {
                    console.log(res)
                    if (res.Code == 0) {
                        console.log(res)
                        window.location.href = "/"
                    } else {
                        layer.msg(res.Msg, {
                            icon: 5
                        });
                    }
                }, 'JSON');
        });
    })

}

/**
 * add_city
 * 
 * 增加一个城市
 * 
 */
function add_city(par_id) {
    layui.use(['layer'], function() {
        var layer = layui.layer
        layer.prompt({
            title: '请输入要添加的城市',
        }, function(value, index, elem) {
            layer.close(index);
            console.log(value)
            console.log(par_id)
            url = '/add_city.php'
            data = {
                'city': value,
                'par_id': par_id
            }
            $.post(
                url,
                data,
                function(res) {
                    if (res.Code == 0) {
                        flush_city(par_id)
                    } else {
                        layer.msg(res.Msg, {
                            icon: 5
                        });
                    }
                }, 'JSON');
        });
    })

}

/**
 * del_city
 * 
 * 删除一个城市
 * 
 */
function del_city(id, par_id) {
    url = '/del_city.php'
    data = {
        'id': id
    }
    $.post(
        url,
        data,
        function(res) {
            console.log(res)
            if (res.Code == 0) {
                console.log(res)
                flush_city(par_id)
                $('#area').empty()
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * edit_city
 * 
 * 编辑一个城市
 * 
 */
function edit_city(id, name, par_id) {
    layui.use(['layer'], function() {
        var layer = layui.layer
        layer.prompt({
            title: '请输入要修改城市的名字',
            value: name
        }, function(value, index, elem) {
            layer.close(index);
            console.log(value)
            url = '/edit_city.php'
            data = {
                'id': id,
                'city': value
            }
            $.post(
                url,
                data,
                function(res) {
                    console.log(res)
                    if (res.Code == 0) {
                        flush_city(par_id)
                    } else {
                        layer.msg(res.Msg, {
                            icon: 5
                        });
                    }
                }, 'JSON');
        });
    })
}

/**
 * add_area
 * 
 * 添加一个市区
 * 
 */
function add_area(par_id) {
    layui.use(['layer'], function() {
        var layer = layui.layer
        layer.prompt({
            title: '请输入要添加的市区',
        }, function(value, index, elem) {
            layer.close(index);
            console.log(value)
            console.log(par_id)
            url = '/add_area.php'
            data = {
                'area': value,
                'par_id': par_id
            }
            $.post(
                url,
                data,
                function(res) {
                    if (res.Code == 0) {
                        flush_area(par_id)
                    } else {
                        layer.msg(res.Msg, {
                            icon: 5
                        });
                    }
                }, 'JSON');
        });
    })
}

/**
 * del_area
 * 
 * 删除一个市区
 * 
 */
function del_area(id, par_id) {
    url = '/del_area.php'
    data = {
        'id': id
    }
    $.post(
        url,
        data,
        function(res) {
            console.log(res)
            if (res.Code == 0) {
                flush_area(par_id)
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * edit_area
 * 
 * 编辑一个市区
 * 
 */
function edit_area(id, name, par_id) {
    layui.use(['layer'], function() {
        var layer = layui.layer
        layer.prompt({
            title: '请输入要修改市区的名字',
            value: name
        }, function(value, index, elem) {
            layer.close(index);
            url = '/edit_area.php'
            data = {
                'id': id,
                'area': value
            }
            $.post(
                url,
                data,
                function(res) {
                    console.log(res)
                    if (res.Code == 0) {
                        flush_area(par_id)
                    } else {
                        layer.msg(res.Msg, {
                            icon: 5
                        });
                    }
                }, 'JSON');
        });
    });
}


/**
 * flush_province
 * 刷新省级列表重新加载数据
 * 
 */
function flush_province() {
    $("#province").empty();
    url = '/flush_provinces.php'
    $.post(
        url, {},
        function(res) {
            if (res.Code == 0) {
                var tmp_province = ''
                res.Data.forEach(row => {
                    tmp_province = tmp_province + "<h4 onclick='showCity(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_city(" +
                        row[0] + ")>+</button><button type='button' onclick=del_province(" + row[0] +
                        ")>-</button><button type='button' onclick=edit_province(" + row[0] + ",'" + row[1] +
                        "')>E</button></span></h4>"
                });
                $("#province").html(tmp_province);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}


/**
 * flush_city
 * 刷新城市列表重新加载数据
 * 
 */
function flush_city(par_id) {
    $("#city").empty();
    url = '/city.php'
    $.post(
        url, {
            'id': par_id
        },
        function(res) {
            if (res.Code == 0) {
                var tmp_province = ''
                res.Data.forEach(row => {
                    tmp_province = tmp_province + "<h4 onclick='showArea(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_area(" +
                        row[0] + ")>+</button><button type='button' onclick=del_city(" + row[0] + "," + row[2] +
                        ")>-</button><button type='button' onclick=edit_city(" + row[0] + ",'" + row[1] + "'," + row[2] +
                        ")>E</button></span></h4>"
                });
                $("#city").html(tmp_province);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * flush_area
 * 刷新市区列表重新加载数据
 * 
 */
function flush_area(par_id) {
    $("#area").empty();
    url = '/area.php'
    $.post(
        url, {
            'id': par_id
        },
        function(res) {
            if (res.Code == 0) {
                var tmp_area = ''
                res.Data.forEach(row => {
                    tmp_area = tmp_area + "<h4>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=del_area(" +
                        row[0] + "," + row[2] + ")>-</button><button type='button' onclick=edit_area(" + row[0] +
                        ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#area").html(tmp_area);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}



/**
 * showCity
 * 显示城市列表 
 * 
 * @param mixed $id 要查询的城市的父级ID
 */
function showCity(id) {
    url = '/city.php'
    data = {
        'id': id
    }
    $.post(
        url,
        data,
        function(res) {
            if (res.Code == 0) {
                $("#city").empty();
                var tmp_city = ''
                res.Data.forEach(row => {
                    tmp_city = tmp_city + "<h4 onclick='showArea(" + row[0] + ")'>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=add_area(" +
                        row[0] + ")>+</button><button type='button' onclick=del_city(" + row[0] +
                        "," + row[2] + ")>-</button><button type='button' onclick=edit_city(" + row[0] +
                        ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#city").html(tmp_city);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}

/**
 * showArea
 * 显示市区列表 
 * 
 * @param mixed $id 要查询的市区的父级ID
 */
function showArea(id) {
    url = '/area.php'
    data = {
        'id': id
    }
    $.post(
        url,
        data,
        function(res) {
            if (res.Code == 0) {
                $("#area").empty();
                var tmp_area = ''
                res.Data.forEach(row => {
                    tmp_area = tmp_area + "<h4>" + row[1] +
                        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span><button type='button' onclick=del_area(" +
                        row[0] + "," + row[2] + ")>-</button><button type='button' onclick=edit_area(" +
                        row[0] + ",'" + row[1] + "'," + row[2] + ")>E</button></span></h4>"
                });
                $("#area").html(tmp_area);
            } else {
                layer.msg(res.Msg, {
                    icon: 5
                });
            }
        }, 'JSON');
}